import OpenAI from 'openai';
import { httpsOverHttp } from 'tunnel';

async function main() {
  // 设置代理服务器
  const tunnel = httpsOverHttp({
    proxy: {
      host: "localhost",
      port: 8080,
    },
  });


  // 调用 OpenAi Api
  const openai = new OpenAI({
    apiKey: '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',
    httpAgent: tunnel,
  });
  try {
    const response = await openai.chat.completions.create({
      model: 'gpt-3.5-turbo',
      messages: [{ role: 'user', content: "你好" }],
    });
    console.log(response?.data?.choices?.[0]?.message?.content?.trim())

  } catch (e) {
    console.error('请检查代理 IP 及端口是否配置正确');
    throw e;
  }

}
main()